package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CROSS_GENERAL_ACTIVITY_UNION_RANK, desc = "跨服活动排名-仅名次")
public class CrossGeneralActivityUnionMyRankCmd extends AbstractCommand {
    private int activityId;
    private int conditionType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdActivityUserRankListReqMsg req = StrongholdWarProto.StrongholdActivityUserRankListReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        conditionType = req.getConditionType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            // 商会分组活动走这边
            if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
                builder.setIdKey(String.valueOf(conditionType));
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_NUM, builder, activityId);
                return;
            }
        }

        StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.Builder msg = StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setConditionType(conditionType);
        UnionActivityConditionData conditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, conditionType, player.getUserId());
        String unionId = "";
        if (conditionData != null) {
            unionId = conditionData.getUnionUid();
        }
        msg.setUnionUid(unionId);
        player.sendPacket(Protocol.C_CROSS_GENERAL_UNION_MY_RANK, msg);
    }



    @Override
    public void sendData(GamePlayer player) {

    }
}
